package com.smart.common.transfer;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.smart.helper.CommonUtil;
import com.smart.model.domain.AccessDO;
import com.smart.model.domain.ManageTempUserRegistrationDO;
import com.smart.model.domain.PortDO;
import com.smart.model.domain.ProjectDO;
import com.smart.service.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Component
public class PortTransfer {
    @Autowired
    private PortService portService;
    @Autowired
    private AccessService accessService;
    @Autowired
    private QrCodeService qrCodeService;
    @Autowired
    private ManageTempUserRegistrationService manageTempUserRegistrationService;

    public void portInfo(Long oldId, ProjectDO projectDO) {
        // 查询老的进出口信息
        List<PortDO> list = portService.lambdaQuery().eq(PortDO::getProjectId, oldId).list();
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        // 写入新的进出口信息
        // key-老的进出口id， value-新的进出口id
        Map<Long, Long> portIdMapping = new HashMap<>();
        for (PortDO portDO : list) {
            Boolean addressType = null;
            if (portDO.getType() == 1) {
                addressType = Boolean.TRUE;
            } else if (portDO.getType() == 2) {
                addressType = Boolean.FALSE;
            }
            Long accessId = qrCodeService.qrCode(null, null, true, projectDO.getId(), portDO.getName(), addressType);
            portIdMapping.put(portDO.getId(), accessId);
        }

        //写入进出口记录
        List<AccessDO> accessList = accessService.lambdaQuery().eq(AccessDO::getProjectId, oldId).list();
        List<ManageTempUserRegistrationDO> registrationList = build(projectDO, portIdMapping, accessList);
        for (ManageTempUserRegistrationDO registrationDO : registrationList) {
            try {
                manageTempUserRegistrationService.save(registrationDO);
            } catch (Exception e) {
                log.error("失败数据 {}", JSON.toJSONString(registrationDO));
                e.printStackTrace();
//                throw e;
            }
        }
    }

    private List<ManageTempUserRegistrationDO> build(ProjectDO projectDO, Map<Long, Long> portIdMapping, List<AccessDO> accessList) {
        List<ManageTempUserRegistrationDO> registrationDOS = Lists.newArrayList();
        for (AccessDO accessDO : accessList) {
            ManageTempUserRegistrationDO registrationDO = new ManageTempUserRegistrationDO();
            registrationDO.setProjectId(projectDO.getId());
            registrationDO.setUserName(accessDO.getName());
            registrationDO.setUserPhone(accessDO.getPhone());
            registrationDO.setIdNumber(accessDO.getIdNumber());
            registrationDO.setAddress(accessDO.getAddr());
            registrationDO.setAddressDetails(accessDO.getAddr());
            registrationDO.setReceiver(accessDO.getReception());
            registrationDO.setLeaveOrNot(0);//默认离场
            if (accessDO.getInTime() !=null) {
                registrationDO.setLeaveOrNot(1);//在场
                registrationDO.setEntranceTime(accessDO.getInTime());
                registrationDO.setEntranceAddress(portIdMapping.get(accessDO.getInPortId()));//进场出口
            }
            if (accessDO.getInTime() !=null && accessDO.getOutTime() !=null && DateUtil.compare(accessDO.getInTime(), accessDO.getOutTime()) < 0) {
                registrationDO.setLeaveOrNot(0);//离场
                registrationDO.setApproachTime(accessDO.getOutTime());
                registrationDO.setApproachAddress(portIdMapping.get(accessDO.getOutPortId()));//离场出口

                registrationDO.setProvince(CommonUtil.provinceMap.get(accessDO.getProvinceCode()));//省
                registrationDO.setCity(CommonUtil.cityMap.get(accessDO.getCityCode()));//市
                registrationDO.setArea(CommonUtil.areaMap.get(accessDO.getAreaCode()));//区
            }
            registrationDOS.add(registrationDO);
        }
        return registrationDOS;
    }
}
